/**
 * 
 */
package com.huyi.lock.zookeeperlock;

import java.util.concurrent.locks.Lock;

/**
 * 描述：	
 * @author shengli.hu
 * @since 1.8	
 * @version 1.0
 */
public class ZookeeperLockDemo {

	static String hostPort = "10.194.80.121:2181";
	static String rootPath = "/zklock";
	
	static int count = 1;
	
	public static void main(String[] args) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + "-运行开始..."); 
				Lock lock = new ZookeeperLock(hostPort, rootPath);
				lock.lock();
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(count > 0) {
					count--;
					System.out.println(Thread.currentThread().getName() + "-购买成功!!!"); 
				} else {
					System.out.println(Thread.currentThread().getName() + "-库存不足！！！"); 
				}
				lock.unlock();
			}
		}, "t1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + "-运行开始..."); 
				Lock lock = new ZookeeperLock(hostPort, rootPath);
				lock.lock();
				try {
					Thread.sleep(4000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(count > 0) {
					count--;
					System.out.println(Thread.currentThread().getName() + "-购买成功!!!"); 
				} else {
					System.out.println(Thread.currentThread().getName() + "-库存不足！！！"); 
				}
				lock.unlock();
			}
		}, "t2").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + "-运行开始..."); 
				Lock lock = new ZookeeperLock(hostPort, rootPath);
				lock.lock();
				try {
					Thread.sleep(4000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(count > 0) {
					count--;
					System.out.println(Thread.currentThread().getName() + "-购买成功!!!"); 
				} else {
					System.out.println(Thread.currentThread().getName() + "-库存不足！！！"); 
				}
				lock.unlock();
			}
		}, "t3").start();
	}
}
